package com.kevin.swing.impl;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j;

import org.apache.commons.lang.StringUtils;

import com.kevin.MainFrame;
import com.kevin.util.CacheUtil;
import com.kevin.util.ConfigProperties;

@EqualsAndHashCode(callSuper = false)
@Log4j
/**
 * 
 * 常用词设置
 * @ClassName ComWordsSetDialog
 * @author <a href="mailto:donggongai@126.com" target="_blank">kevin</a>
 * @date 2016年9月24日 上午10:44:11
 *
 */
public @Data class ComWordsSetDialog extends JDialog {
    private MainFrame owner;

    /**
	 * 
	 */
    private static final long serialVersionUID = 6792501234970255123L;

    private JLabel wordSetLabel;
    private JTextArea wordSetTA;
    private JScrollPane wordSetJsp;

    private JPanel btnPanel = new JPanel();
    private JButton okbtn = new JButton("保存");
    private JButton clbtn = new JButton("取消");
    private String wordSetStr = "";
    private String msg = "";

    public ComWordsSetDialog(MainFrame owner) {
        super();
        this.owner = owner;
        wordSetLabel = new JLabel("<html>此设置用于转换为java字段时显示,格式：username=userName;createtime=createTime;</html>");
        wordSetTA = new JTextArea();
        wordSetTA.setLineWrap(true);
        wordSetTA.setBorder(BorderFactory.createTitledBorder("常用词"));

        loadDefaultSet();

        wordSetJsp = new JScrollPane(wordSetTA);
        wordSetJsp.setBorder(null);
        okbtn = new JButton("确定");
        clbtn = new JButton("取消");

        btnPanel = new JPanel();
        btnPanel.add(okbtn);
        btnPanel.add(clbtn);

        setTitle("常用词设置");
        add(wordSetLabel, BorderLayout.NORTH);
        add(wordSetJsp, BorderLayout.CENTER);
        add(btnPanel, BorderLayout.SOUTH);
        setVisible(false);
        setSize(400, 200);
        setResizable(false);
        init();
    }

    /**
     * 加载默认设置
     * 
     * @Title loadDefaultSet
     * @author 吕凯
     * @date 2016年8月27日 上午11:14:27 void
     */
    private void loadDefaultSet() {
        wordSetStr = "";
        Map<String, String> commonWordsMapFromCache = new LinkedHashMap<>();
        try {
            commonWordsMapFromCache = CacheUtil.get(ConfigProperties.CACHE_COMMON_WORDS);
        } catch (Exception e) {
            log.error("获取常用词设置缓存失败", e);
            ;
        }
        if (commonWordsMapFromCache != null) {
            commonWordsMapFromCache.forEach((key, value) -> {
                wordSetStr += key + "=" + value + ";";
            });
        }
        wordSetTA.setText(wordSetStr);
    }

    private void init() {
        clbtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                loadDefaultSet();
                ComWordsSetDialog.this.setVisible(false);
            }
        });

        okbtn.addActionListener(e -> {
            String wordset = getWordSetTA().getText();
            String[] wordSetValues = wordset.split(";");
            if (wordSetValues != null && wordSetValues.length > 0) {
                Map<String, String> commonWordsMap = new LinkedHashMap<>();
                String cacheTableColData = CacheUtil.getString(ConfigProperties.CACHE_TABLE_COL);
                msg = "";
                Arrays.asList(wordSetValues).forEach(wordSetSplit -> {
                    String[] wordSetkeyValue = wordSetSplit.split("=");
                    if (wordSetkeyValue.length == 2) {
                        commonWordsMap.put(wordSetkeyValue[0].toLowerCase(), wordSetkeyValue[1]);// key小写
                        if (!StringUtils.isBlank(cacheTableColData)) {
                            if (cacheTableColData.contains("\"columnName\":\"" + wordSetkeyValue[0].toLowerCase() + "\"")
                                    && !cacheTableColData.contains("\"exportColName\":\"" + wordSetkeyValue[1] + "\"")) {
                                msg += " " + wordSetkeyValue[0].toLowerCase();
                            }
                        }
                    }
                });
                CacheUtil.put(ConfigProperties.CACHE_COMMON_WORDS, commonWordsMap);
                if (!StringUtils.isBlank(msg)) {
                    JOptionPane.showMessageDialog(ComWordsSetDialog.this, "检查到最后一步的缓存字段中有跟配置不一致的地方，请注意“重新载入”。字段：" + msg);
                }
                loadDefaultSet();// 载入新配置
            } else {
                CacheUtil.put(ConfigProperties.CACHE_COMMON_WORDS, "");
            }
            ComWordsSetDialog.this.setVisible(false);
        });

    }

}
